package com.hy.admin.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hy.admin.entity.YangCoinEntity;
import com.hy.admin.helper.request.PersonalRequest;
import com.hy.admin.helper.request.RechargeRequest;
import com.hy.admin.helper.request.YangCoinPayRequest;
import com.hy.admin.helper.response.MemberYangCoinDetailResponse;
import com.hy.admin.helper.response.OrderResponse;
import com.hy.admin.service.YangCoinService;
import com.hy.core.request.ApiRequestDTO;
import com.hy.core.response.ApiResponseDTO;
import com.hy.core.response.ServicesResult;
import org.apache.commons.httpclient.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by yangqq on 18/4/3.
 */
@RestController
@RequestMapping("/api/coin")
public class YangCoinController {

    protected final Logger logBase = LoggerFactory.getLogger(YangCoinController.class);
    @Autowired
    private YangCoinService yangCoinService;

    /**
     * 氧币列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiResponseDTO list(@RequestBody final ApiRequestDTO apiRequestDTO,HttpServletRequest request) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("yang_coin_list_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        //获取请求数据
        PersonalRequest personalRequest= gson.fromJson(apiRequestDTO.getRequestData(), PersonalRequest.class);
        ServicesResult<List<YangCoinEntity>> yangCoinResponse = yangCoinService.list();
        apiResponseDTO.initServicesResult(yangCoinResponse);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(yangCoinResponse.getResult()));
        logBase.info("yang_coin_list_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    /**
     * 氧币明细列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    public ApiResponseDTO detail(@RequestBody final ApiRequestDTO apiRequestDTO,HttpServletRequest request) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("yang_coin_detail_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        //获取请求数据
        PersonalRequest personalRequest= gson.fromJson(apiRequestDTO.getRequestData(), PersonalRequest.class);
        ServicesResult<List<MemberYangCoinDetailResponse>> yangCoinResponse = yangCoinService.detail(personalRequest.getMemberId(), personalRequest.getPageNum(), personalRequest.getPageSize());
        apiResponseDTO.initServicesResult(yangCoinResponse);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(yangCoinResponse.getResult()));
        logBase.info("yang_coin_detail_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    /**
     * 氧币充值
     * @param request
     * @return
     */
    @RequestMapping(value = "/recharge", method = RequestMethod.POST)
    public ApiResponseDTO recharge(@RequestBody final ApiRequestDTO apiRequestDTO,HttpServletRequest request) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("yang_coin_recharge_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        //获取请求对象
        RechargeRequest rechargeRequest= gson.fromJson(apiRequestDTO.getRequestData(), RechargeRequest.class);
        //获取请求数据
        ServicesResult<OrderResponse> rechargeResponse = yangCoinService.recharge(rechargeRequest.getMemberId(), rechargeRequest.getCoinId());
        apiResponseDTO.initServicesResult(rechargeResponse);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(rechargeResponse.getResult()));
        logBase.info("yang_coin_recharge_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    /**
     * 氧币充值成功
     * @param request
     * @return
     */
    @RequestMapping(value = "/paySuccess", method = RequestMethod.POST)
    public ApiResponseDTO paySuccess(@RequestBody final ApiRequestDTO apiRequestDTO,HttpServletRequest request) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("yang_coin_paySuccess_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        //获取请求对象
        RechargeRequest rechargeRequest= gson.fromJson(apiRequestDTO.getRequestData(), RechargeRequest.class);
        //获取请求数据
        ServicesResult<Boolean> rechargeResponse;
        try {
            rechargeResponse = yangCoinService.paySuccess(rechargeRequest.getOrderId());
        } catch (Exception e) {
            e.printStackTrace();
            rechargeResponse = new ServicesResult<>();
            rechargeResponse.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            rechargeResponse.setStatusMsg("系统异常");
        }
        apiResponseDTO.initServicesResult(rechargeResponse);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(rechargeResponse.getResult()));
        logBase.info("yang_coin_paySuccess_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    /**
     * 氧币支付--购买课程
     * @param request
     * @return
     */
    @RequestMapping(value = "/pay", method = RequestMethod.POST)
    public ApiResponseDTO pay(@RequestBody final ApiRequestDTO apiRequestDTO,HttpServletRequest request) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("yang_coin_pay_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        //获取请求对象
        YangCoinPayRequest yangCoinPayRequest= gson.fromJson(apiRequestDTO.getRequestData(), YangCoinPayRequest.class);
        //获取请求数据
        ServicesResult<Boolean> payResponse;
        try {
            payResponse = yangCoinService.pay(yangCoinPayRequest.getCourseOrderId(), yangCoinPayRequest.getCoinCount());
        } catch (Exception e) {
            e.printStackTrace();
            payResponse = new ServicesResult<>();
            payResponse.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            payResponse.setStatusMsg("系统异常");
        }
        apiResponseDTO.initServicesResult(payResponse);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(payResponse.getResult()));
        logBase.info("yang_coin_pay_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }
    /**
     * 氧币支付--查看答案
     * @param request
     * @return
     */
    @RequestMapping(value = "/payAnswer", method = RequestMethod.POST)
    public ApiResponseDTO payAnswer(@RequestBody final ApiRequestDTO apiRequestDTO,HttpServletRequest request) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("yang_coin_payAnswer_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        //获取请求对象
        YangCoinPayRequest yangCoinPayRequest= gson.fromJson(apiRequestDTO.getRequestData(), YangCoinPayRequest.class);
        //获取请求数据
        ServicesResult<Boolean> payResponse;
        try {
            payResponse = yangCoinService.payAnswer(yangCoinPayRequest.getMemberId(), yangCoinPayRequest.getQuestionId(), yangCoinPayRequest.getCoinCount());
        } catch (Exception e) {
            e.printStackTrace();
            payResponse = new ServicesResult<>();
            payResponse.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            payResponse.setStatusMsg("系统异常");
        }
        apiResponseDTO.initServicesResult(payResponse);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(payResponse.getResult()));
        logBase.info("yang_coin_payAnswer_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }
}
